﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibUtilities.dal;
using System.Data;
using System.Data.SqlClient;

namespace CLibArtreve.dal
{
    public class CategoryDAL : SqlDatabase
    {
        #region CONSTRUCTORS
        public CategoryDAL(string connectionString) : base(connectionString) { }
        #endregion

        #region OVERRIDE METHODS
        public override string ToString()
        {
            return "DATA ACCES LAYER: " + this.GetType().ToString();
        }
        #endregion

        #region GET METHODS
        public DataSet GetAllCategories()
        {
            try
            {
                return ExecuteDataSetWithCommonQuery(Queries.SELECTALLCATEGORIES, null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataSet GetCategoryById(Int64? id)
        {
            try
            {
                // MAKE A LIST OF PARAMETERS
                List<SqlParameter> parameters = new List<SqlParameter>();

                // MAKE PARAMETER
                SqlParameter parameter = new SqlParameter();
                parameter.DbType = DbType.Int64;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "category_id";
                parameter.Value = id;
                parameters.Add(parameter);

                // EXECUTE
                return ExecuteDataSetWithCommonQuery(Queries.SELECTCATEGORYBYID, parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public DataSet GetCategoryByArtId(Int64? id)
        {
            try
            {
                // MAKE A LIST OF PARAMETERS
                List<SqlParameter> parameters = new List<SqlParameter>();

                // MAKE PARAMETER
                SqlParameter parameter = new SqlParameter();
                parameter.DbType = DbType.Int64;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "arts_art_id";
                parameter.Value = id;
                parameters.Add(parameter);

                // EXECUTE
                return ExecuteDataSetWithCommonQuery(Queries.SELECTCATEGORYBYARTID, parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public DataSet GetCategoryByUserId(Int64? id)
        {
            try
            {
                // MAKE A LIST OF PARAMETERS
                List<SqlParameter> parameters = new List<SqlParameter>();

                // MAKE PARAMETER
                SqlParameter parameter = new SqlParameter();
                parameter.DbType = DbType.Int64;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "user_id";
                parameter.Value = id;
                parameters.Add(parameter);

                // EXECUTE
                return ExecuteDataSetWithCommonQuery(Queries.SELECTALLCATEGORIESBYUSERID, parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region INSERT METHODS
        public int InsertCategory(string categoryName, string categoryDescription, string categoryImgUrl)
        {
            try
            {
                //MAKE SOME PARAMETERS
                List<SqlParameter> parameters = new List<SqlParameter>();

                //MAKE PARAMETERS
                SqlParameter parameter = new SqlParameter();
                parameter.DbType = DbType.String;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "category_name";
                parameter.Value = categoryName;
                parameters.Add(parameter);

                parameter = new SqlParameter();
                parameter.DbType = DbType.String;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "category_description";
                parameter.Value = categoryDescription;
                parameters.Add(parameter);

                parameter = new SqlParameter();
                parameter.DbType = DbType.String;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "category_imgUrl";
                parameter.Value = categoryImgUrl;
                parameters.Add(parameter);

                // EXECUTE
                int p_category = ExecuteNonQueryWithCommonQuery(Queries.INSERTCATEGORY, parameters, true);
                if (p_category > 0)
                {
                    p_category = Convert.ToInt32(ExecuteScalarWithCommonQuery<decimal>(Queries.SELECTIDENTITYAFTERINSERTSQLSERVER));
                }
                return p_category;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        #endregion
    }
}
